/**推送处理文件，该文件由home.html 页引用**/
//设置当前设备的tags 和alias
//window.plugins.jPushPlugin.setTagsWithAlias(["111","2"], "asdasd");//tags数组,Alias数组
var app = {
	initialize : function() {
		document.addEventListener('deviceready', this.DeviceReady, false);
	},
	DeviceReady : function() {
		onDeviceReady();
	},
	receivedEvent : function() {
		// 设置标签和别名
		document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
		// 打开通知
		document.addEventListener("jpush.openNotification", onOpenNotification, false);
		// 获取通知内容
		document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
		// 获取自定义消息推送内容
		document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
	},
	//获取注册ID
	getRegistrationID : function() {
		window.plugins.jPushPlugin.getRegistrationID(onGetRegistrationID);
	}
};
// 开始
var onDeviceReady = function() {
	try {
		app.receivedEvent();
		//初始化
		window.plugins.jPushPlugin.init();
		app.getRegistrationID();
		if (device.platform != "Android") {
			window.plugins.jPushPlugin.setDebugModeFromIos();
			window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
		} else {
			window.plugins.jPushPlugin.setDebugMode(true);
			window.plugins.jPushPlugin.setStatisticsOpen(true);
		}
	} catch (exception) {
		console.log(exception);
	}
};

// 得到id
var onGetRegistrationID = function(data) {
	try {
		if (data.length == 0) {
			var t1 = window.setTimeout(app.getRegistrationID, 1000);
			return;
		}
		deviceid = data;

		//data
	} catch (exception) {
		alert(exception);
	}
};
// 设置标签和别名
var onTagsWithAlias = function(event) {
	return;
	//不处理
	try {
		var result = "result code:" + event.resultCode + " ";
		result += "tags:" + event.tags + " ";
		result += "alias:" + event.alias + " ";
		alert("即将把tags和alias显示在页面上");
		$("#tagAliasResult").html(result);
	} catch (exception) {
		alert(exception)
	}
};
// 打开通知
var onOpenNotification = function(events) {
	events = JSON.parse(events.extras.data);

	try {
		var alertContent = "";
		//if (device.platform == "Android") {
		//    alertContent = event.alert;
		//} else {
		//    alertContent = event.aps.alert;
		//}

		var dat = {
			context : alertContent, //通知内容
			messageId : events.DocNum, //通知编号
			thistime : "", //通知时间
			type : events.PushType,//通知类型
		};
		MessageJunp(dat);
	} catch (exception) {
		alert("打开通知时出现错误" + exception);
	}
};
//获取通知内容
var onReceiveNotification = function(event) {
	
	try {
		var alertContent;
		if (device.platform == "Android") {
			alertContent = event.alert;
		} else {
			alertContent = event.aps.alert;
		}
	} catch (exception) {
		alert(exception)
	}
};
// 获取自定义消息推送内容
var onReceiveMessage = function(event) {
	try {
		var message;
		if (device.platform == "Android") {
			message = event.message;
		} else {
			message = event.content;
		}
		//alert("把自定义的消息显示在页面上");
		//$("#messageResult").html(message);
	} catch (exception) {
		alert("JPushPlugin:onReceiveMessage-->" + exception);
	}
};
function summerreadys() {
	app.initialize();
}